init('Collections');
(function(){
	var iterate = function(list, callback, scope){
		if( !scope ) scope = this;
		if( !list ) return;
		if( typeof list == 'Array' || list.length || list.length === 0){
			for(var index=0; index < list.length; index++)
				callback.apply(scope, [list[index], index]);
		}else{
			for(var field in list){
				callback.apply(scope, [list[field], field]);
			}
		}
	};
	var filter = function(collection, pattern, _toString){
		var results = [];
		iterate(collection, function(item){
			var str = item;
			if( _toString ) str = _toString(item);
			if( str && str.match(pattern) ) results.push(item);
		});
		return results;
	};
	var toArray = function(string, delimiter, escape){
		if( !delimiter ) delimiter = ' ';
		var commandAndArgs = string.split(delimiter);
		var _args = [];
		if( commandAndArgs.length >= 1 ){
			var tmp = '';
			iterate(commandAndArgs, function(arg, index){
				if( arg.match(/^['"]/) ){
					tmp = arg;
				}else if( arg.match(/['"]$/) ){
					tmp += " " + arg;
					_args.push(tmp);
					tmp = '';
				}else if( !tmp ){
					_args.push(arg);
				}else{
					tmp += " " + arg;
				}
			});
			if( tmp ) _args.push(tmp);
		}
		return _args;
	};
	var contains = function(collection, item){
		var contains = false;
		iterate(collection, function(value){
			if( value == item ) contains = true;
		});
		return contains;
	};
	exports.contains = contains;
	
	var matchAll = function(collection, pattern, _toString){
		return filter(collection,pattern, _toString).length == collection.length;
	};
	exports.matchAll = matchAll;
	
	var max = function(collection, _toString){
		var result;
		iterate(collection, function(item){
			if( !result ) result = item;
			if( _toString(item).length > _toString(result).length ) result = item;
		});
		return result;
	};
	exports.max = max;
	
	var combine = function(collection, spacer, emptyCollectionMessage){
		var result = '';
		var empty  = true;
		iterate(collection, function(item){
			empty = false;
			if(result && spacer) result += spacer;
			result += item; 
		});
		if( empty ){
			result = emptyCollectionMessage;
		}
		return result;
	};
	exports.combine = combine;
	
	var gcs = function(collection, _toString){
		if(collection.length == 0) return '';
		if(!_toString) _toString = function(item){ return item; }
		var maxItem = max(collection, _toString);
		var maxstr = _toString(maxItem);
		var last = '';
		for(var i=1; i < maxstr.length; i++){
			var sub = maxstr.substring(0, i);
			if(!matchAll(collection, '^' + sub, _toString))
				return last;
			last = sub;
		}
		return maxstr;
	};
	exports.gcs = gcs;
	
	exports.toArray = toArray;
	exports.iterate = iterate;
	exports.filter  = filter; 
	
	var Map = {};
	Map.putAll = function(map1, map2, converter){
		if(map2)
			iterate(map2, function(item, name){
				if( converter ){
					var pair = converter(item, name);
					map1[pair.key] = pair.value;
				}else{
					map1[name] = item;
				} 
			})
		return map1;
	}
	Map.keySet = function(map, converter){
		var keys = []
		if( map ){
			iterate(map, function(item, key){
				keys.push(converter? converter(key) : key);
			})
		}
		return keys;
	}
	Map.values = function(map, converter){
		var values = []
		if( map ){
			iterate(map, function(item, key){
				values.push(converter? converter(item) : item);
			})
		}
		return values;
	}
	exports.Map = Map;
	
	var List = {};
	List.addAll = function(list1, list2, converter){
		if(list2)
			iterate(list2, function(item){ list1.push(converter?converter(item):item); });
		return list1;
	}
	List.clean = function(list){
		var store = []
		iterate(list, function(item){
			if(item)
				store.push(item);
		});
		return store;
	}
	List.contains = function(list, item){
		var match = false;
		iterate(list, function(item2){
			match = match || item === item2;
		});
		return match;
	}
	List.remove = function(list, item){
		iterate(list, function(item1, index){
			if(item == item1)
				list[index] = null;
		});
		return List.clean(list);
	};
	exports.List = List;
	
})();